package com.bluetoothsms.common;

public class Conversation implements Comparable<Conversation> {

	public final static int FIELD_COUNT = 5;

	private final String mPhoneNumber;
	private final String mContactName;
	private final int mThreadId;
	private final int mMsgCount;
	private final String mSnippet;

	public Conversation(String number, String name, int threadId, int msgCount,
			String snippet) {
		mPhoneNumber = number;
		mContactName = name;
		mThreadId = threadId;
		mMsgCount = msgCount;
		mSnippet = snippet;
	}

	public Conversation(String[] data, int firstIdx) {
		if (data.length - firstIdx < FIELD_COUNT)
			throw new IllegalArgumentException("Too few data fields!");

		mPhoneNumber = data[firstIdx++];
		mContactName = data[firstIdx++];
		mThreadId = Integer.valueOf(data[firstIdx++]);
		mMsgCount = Integer.valueOf(data[firstIdx++]);
		mSnippet = data[firstIdx++];
	}

	public String getPhoneNumber() {
		return mPhoneNumber;
	}

	public String getContactName() {
		return mContactName;
	}

	public int getThreadId() {
		return mThreadId;
	}

	public int getMessageCount() {
		return mMsgCount;
	}

	public String getSnippet() {
		return mSnippet;
	}

	public String toTitleString() {
		if (mContactName == null || mContactName.isEmpty())
			return mPhoneNumber;
		else
			return mContactName;
	}

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(toTitleString());
		b.append(" [");
		b.append(mMsgCount);
		b.append("] ");

		if (mSnippet != null)
			b.append(mSnippet);

		return b.toString();
	}

	public int compareTo(Conversation o) {
		if (o == null)
			return 0;

		return toTitleString().compareToIgnoreCase(o.toTitleString());
	}

}
